(function($){
	$(document).ready(function () {
		$('.btnTgle').click(onBtnTgle);
		$('#frmGroup').submit(onSearch);
		$('#frmPeople').submit(onPeopleSearch);
		window.postPerGroup = 2;
		window.userPerPost = 20;
		
		// for testing
		// $('#key').val('võ hoài linh');
		// $('#frmGroup').submit();
	});

})(jQuery);

// search group form submit
function onSearch(e) {
	e.preventDefault();
	var q = $(this).serialize();
	var loading = $('.group-panel').find('.loading');
	loading.show();
	$('#groupGrid').html('');

	$('#curQuery').val('');
	$('#objectIDs').val('');

	$.ajax({
		type: "GET",
		url: baseUrl+'/site/search?'+q,
		success: function (data) {
			$('#groupGrid').html(data);
			$('#curQuery').val(q);
			setCheckboxState();
			loading.hide();
		}
	});
}

// next/previous button click
function loadPage(index) {
	var q = $('#curQuery').val() + '&offset=' + index;
	var loading = $('.group-panel').find('.loading');
	loading.show();
	$('#groupGrid').html('');

	$.ajax({
		type: "GET",
		url: baseUrl+'/site/search?'+q,
		success: function (data) {
			$('#groupGrid').html(data);
			setCheckboxState();
			loading.hide();
		}
	});
	
	return false;
}

// get checkbox state for grid
function setCheckboxState() {
	var val = $('#objectIDs').val();
	var ar = val.trim()!='' ? val.split(', ') : [];

	$('.chkItem').each(function() {
		if (ar.indexOf(this.value)>-1)
			this.checked = true;
	});
	
	setGridButonsState();
}

// checkbox click event
function onItemClick() {
	var val = $('#objectIDs').val();
	var ar = val.trim()!='' ? val.split(', ') : [];
	
	if ($(this).is(':checked')) {
		// add item to list
		ar.push(this.value);
	} else {
		// remove item from list
		var index = ar.indexOf(this.value);
		if (index > -1) {
			ar.splice(index, 1);
		}
	}

	$('#objectIDs').val(ar.length>0 ? ar.join(', ') : '');
	setGridButonsState();
}

// toggle advance search form
function onBtnTgle() {
	$('.more-fields').toggle();
	$(this).toggleClass('glyphicon-chevron-down').toggleClass('glyphicon-chevron-up');
}

// set grid action buttons to enable/disable
function setGridButonsState() {
	var val = $('#objectIDs').val();
	var ar = val.trim()!='' ? val.split(', ') : [];

	if (ar.length==0) {
		$('.group-panel .actions button').each(function () { $(this).attr('disabled', 'disabled'); });
	} else {
		$('.group-panel .actions button').each(function () { $(this).removeAttr('disabled'); });
	}
}

function onBtnPeople() {
	$('#groupSection').collapse('hide');
	$('#peopleSection').collapse('show');
	$('#topic').focus();
}

function onPeopleSearch(e) {
	e.preventDefault();

	var val = $('#objectIDs').val();
	var object_ids = val.trim()!='' ? val.split(', ') : [];
	var s = $('#topic').val();
	
	$(object_ids).each(function() {
		findPost(s, this, 0);
	});
}

function findPost(s, obj, off) {
	var q = 's='+encodeURI(s)+'&obj='+obj+'&off='+off;
	$.ajax({
		type: "GET",
		url: baseUrl+'/site/searchPosts?'+q,
		dataType : 'json',
		success: function (data) {
			var pids = [];
			$(data.items).each(function() {
				pids.push(this.post_id);
			});
			getUsers(pids, 0);
			
			if ('next' in data.paging)
				findPost(s, obj, data.paging.next);
		}
	});
}

function getUsers(pids, off) {
	var q = 'p='+pids+'&off='+off;
	$.ajax({
		type: "GET",
		url: baseUrl+'/site/getUsers?'+q,
		dataType : 'json',
		success: function (data) {
			$(data.items).each(function() {
				addUserToView(this);
			});
			
			if ('next' in data.paging)
				getUsers(pids, data.paging.next);
		}
	});
}

function addUserToView(u) {
	var html = 
	'<div class="media">'+
		'<a href="'+u.profile_url+'" class="pull-left" target="_blank">'+
			'<img alt="avatar" class="media-object" style="width: 64px;" src="'+u.pic_small+'">'+
		'</a>'+
		'<div class="media-body">'+
			'<h4 class="media-heading"><a href="'+u.profile_url+'" class="pull-left" target="_blank">'+
				u.first_name+' '+u.middle_name+' '+u.last_name+
			'</a></h4>'+
		'</div>'+
	'</div>';
	$('#people').append(html);
}